<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>商品列表</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
        }
        .product-card {
            transition: all 0.3s ease;
            height: 100%;
            border: none;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        }
        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 16px rgba(0,0,0,0.1);
        }
        .product-image {
            height: 200px;
            object-fit: cover;
            width: 100%;
            border-radius: 8px 8px 0 0;
        }
        .product-image-container {
            position: relative;
            overflow: hidden;
            height: 200px;
            background-color: #f8f9fa;
            border-radius: 8px 8px 0 0;
        }
        .no-image-placeholder {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100%;
            color: #6c757d;
            font-size: 1.2rem;
            background: linear-gradient(45deg, #f8f9fa, #e9ecef);
        }
        .category-badge {
            position: absolute;
            top: 10px;
            right: 10px;
            background: rgba(13, 110, 253, 0.9);
            color: white;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 0.8rem;
            backdrop-filter: blur(4px);
        }
        .filter-section {
            background: white;
            padding: 25px;
            border-radius: 12px;
            margin-bottom: 30px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        }
        .parent-categories {
            margin-bottom: 1.5rem;
            display: flex;
            flex-wrap: wrap;
            gap: 0.5rem;
        }
        .parent-category-btn {
            border-radius: 20px;
            padding: 8px 16px;
            transition: all 0.3s ease;
        }
        .parent-category-btn:hover {
            transform: translateY(-2px);
        }
        .parent-category-btn.active {
            background-color: #0d6efd;
            color: white;
            box-shadow: 0 4px 8px rgba(13, 110, 253, 0.2);
        }
        .sub-categories {
            display: none;
            margin-top: 1rem;
            padding: 1rem;
            background: #f8f9fa;
            border-radius: 8px;
        }
        .sub-categories.active {
            display: block;
            animation: fadeIn 0.3s ease;
        }
        .sub-category-btn {
            margin: 0.25rem;
            border-radius: 15px;
            font-size: 0.9rem;
            padding: 6px 12px;
        }
        .price-filter {
            background: #f8f9fa;
            padding: 1rem;
            border-radius: 8px;
            margin-top: 1rem;
        }
        .card-body {
            padding: 1.25rem;
        }
        .card-title {
            font-size: 1.1rem;
            font-weight: 600;
            margin-bottom: 0.75rem;
            color: #2c3e50;
        }
        .card-text {
            color: #6c757d;
            font-size: 0.9rem;
            margin-bottom: 1rem;
            height: 3em;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }
        .price-stock {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 1rem;
            padding: 0.5rem 0;
            border-top: 1px solid #eee;
            border-bottom: 1px solid #eee;
        }
        .price {
            font-size: 1.2rem;
            font-weight: bold;
            color: #e74c3c;
        }
        .stock {
            color: #7f8c8d;
            font-size: 0.9rem;
        }
        .detail-btn {
            border-radius: 20px;
            width: 100%;
            padding: 8px;
            transition: all 0.3s ease;
        }
        .detail-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(13, 110, 253, 0.2);
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-10px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .section-title {
            position: relative;
            margin-bottom: 2rem;
            text-align: center;
            color: #2c3e50;
        }
        .section-title:after {
            content: '';
            display: block;
            width: 50px;
            height: 3px;
            background: #0d6efd;
            margin: 10px auto;
            border-radius: 2px;
        }

    </style>
</head>
<body data-user-role="customer">
<!-- 引入增强版全局通知组件 -->
<div th:replace="~{common/enhanced-global-notification :: notification}"></div>

<div th:replace="~{common/customer-navbar :: navbar}"></div>

<div class="container mt-4">
    <h2 class="section-title">商品列表</h2>

    <!-- 筛选区域 -->
    <div class="filter-section">
        <form th:action="@{/customer/products}" method="get" id="filterForm">
            <input type="hidden" id="categoryId" name="categoryId" th:value="${selectedCategory}">
            <input type="hidden" id="parentCategoryId" name="parentCategoryId" th:value="${selectedParentCategory}">

            <div class="mb-4">
                <h5 class="mb-3">选择分类</h5>
                <!-- 父分类按钮组 -->
                <div class="parent-categories">
                    <button type="button" class="btn btn-outline-primary parent-category-btn"
                            th:classappend="${selectedParentCategory == null ? 'active' : ''}"
                            data-category-id="">全部</button>
                    <button type="button"
                            th:each="parent : ${parentCategories}"
                            class="btn btn-outline-primary parent-category-btn"
                            th:classappend="${parent.categoryId == selectedParentCategory ? 'active' : ''}"
                            th:data-category-id="${parent.categoryId}"
                            th:text="${parent.categoryName}">
                        父分类名称
                    </button>
                </div>

                <!-- 子分类按钮组 -->
                <div th:each="parent : ${parentCategories}"
                     th:id="'sub-categories-' + ${parent.categoryId}"
                     class="sub-categories"
                     th:classappend="${parent.categoryId == selectedParentCategory ? 'active' : ''}">
                    <button type="button" class="btn btn-outline-secondary sub-category-btn"
                            th:classappend="${selectedCategory == null ? 'active' : ''}"
                            data-category-id="">全部</button>
                    <button type="button"
                            th:each="child : ${subCategoriesMap?.get(parent.categoryId)}"
                            class="btn btn-outline-secondary sub-category-btn"
                            th:classappend="${child.categoryId == selectedCategory ? 'active' : ''}"
                            th:data-category-id="${child.categoryId}"
                            th:text="${child.categoryName}">
                        子分类名称
                    </button>
                </div>
            </div>

            <div class="price-filter">
                <h5 class="mb-3">价格区间</h5>
                <div class="row g-3 align-items-center">
                    <div class="col">
                        <div class="input-group">
                            <span class="input-group-text">¥</span>
                            <input type="number" class="form-control" id="minPrice" name="minPrice"
                                   th:value="${param.minPrice}" placeholder="最低价">
                        </div>
                    </div>
                    <div class="col-auto">
                        <span class="form-text">至</span>
                    </div>
                    <div class="col">
                        <div class="input-group">
                            <span class="input-group-text">¥</span>
                            <input type="number" class="form-control" id="maxPrice" name="maxPrice"
                                   th:value="${param.maxPrice}" placeholder="最高价">
                        </div>
                    </div>
                    <div class="col-auto">
                        <button type="submit" class="btn btn-primary">应用筛选</button>
                        <a href="/customer/products" class="btn btn-outline-secondary">重置</a>
                    </div>
                </div>
            </div>
        </form>
    </div>

    <!-- 商品列表 -->
    <div class="row g-4">
        <div class="col-md-4" th:each="product : ${products}">
            <div class="card product-card">
                <div class="product-image-container">
                    <img th:if="${product.photo != null and !product.photo.isEmpty()}"
                         th:src="${product.photo}"
                         class="product-image"
                         th:alt="${product.productName}">
                    <div th:unless="${product.photo != null and !product.photo.isEmpty()}"
                         class="no-image-placeholder">
                        <i class="fas fa-image me-2"></i> 暂无图片
                    </div>
                    <div class="category-badge">
                        <span th:text="${product.parentCategoryName + ' > ' + product.categoryName}">分类</span>
                    </div>
                </div>
                <div class="card-body">
                    <h5 class="card-title" th:text="${product.productName}">商品名称</h5>
                    <p class="card-text" th:text="${product.description}">商品描述</p>
                    <div class="price-stock">
                        <span class="price" th:text="'¥' + ${product.price}">¥99.00</span>
                        <span class="stock" th:text="'库存: ' + ${product.rest}">库存: 10</span>
                    </div>
                    <div class="d-grid gap-2">
                        <a th:href="@{'/customer/products/' + ${product.productId}}"
                           class="btn btn-primary detail-btn">查看详情</a>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/js/all.min.js"></script>
<script th:inline="javascript">
$(document).ready(function() {
    // 父分类按钮点击事件
    $('.parent-category-btn').click(function() {
        const parentId = $(this).data('category-id');

        // 更新按钮状态
        $('.parent-category-btn').removeClass('active');
        $(this).addClass('active');

        // 更新隐藏字段
        $('#parentCategoryId').val(parentId);
        $('#categoryId').val('');

        // 隐藏所有子分类
        $('.sub-categories').removeClass('active');

        // 如果选择了特定父分类，显示对应子分类
        if (parentId) {
            $('#sub-categories-' + parentId).addClass('active');
        }

        // 提交表单
        $('#filterForm').submit();
    });

    // 子分类按钮点击事件
    $('.sub-category-btn').click(function() {
        const categoryId = $(this).data('category-id');

        // 更新按钮状态
        $(this).closest('.sub-categories').find('.sub-category-btn').removeClass('active');
        $(this).addClass('active');

        // 更新隐藏字段
        $('#categoryId').val(categoryId);

        // 提交表单
        $('#filterForm').submit();
    });
});
</script>
</body>
</html>